import axios, { type AxiosRequestHeaders, type AxiosResponse } from 'axios'
// import { useUserInfoStore } from '../stores/userInfo'
// import pinia from '@/stores/userInfo'
import nprogress from 'nprogress'
import 'nprogress/nprogress.css'
import { getToken, getUserTempId } from './auth'
// import pinia from '@/stores/userInfo'
// import { useUserInfoStore } from '../stores/userInfo'
interface ResponseData<T> {
  code: number
  data: T
  message: string
}

// 创建 axios 实例
const sphRequest = axios.create({
  baseURL: '/sph',
  timeout: 10000
})

// 请求拦截器
sphRequest.interceptors.request.use(
  (config) => {
    // 启动进度条
    nprogress.start()
    const token = localStorage.getItem('token')
    if (token) {
      ;(config.headers as AxiosRequestHeaders).token = token
    }
    if (token && token !== '') {
      ;(config.headers as AxiosRequestHeaders).token = token
    }
    ;(config.headers as AxiosRequestHeaders).userTempId = getUserTempId()

    return config
  },
  (error) => {
    return Promise.reject(error)
  }
)

// 响应拦截器，直接取出响应体
sphRequest.interceptors.response.use(
  (response) => {
    // 关闭进度条
    nprogress.done()
    return response.data
  },

  (error) => {
    return Promise.reject(error)
  }
)

export default sphRequest
